package com.zzptc.sky.waterlifer;

import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }
    //初始数据
    public void initData(){

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                //访问网络的方法
//                try {
//                    String str=getJSON();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://192.168.1.118:8080")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        aaa a=retrofit.create(aaa.class);
        Call<List<Student>> studentCall=a.getStuInfo();
        studentCall.enqueue(new Callback<List<Student>>() {

            @Override
            public void onResponse(Call<List<Student>> call, Response<List<Student>> response) {
                List<Student> list=response.body();
                for (int i=0;i<list.size();i++){
                    System.out.println(list.get(i).getStu_name());
                }
            }

            @Override
            public void onFailure(Call<List<Student>> call, Throwable t) {

            }
        });

    }
    public String getJSON() throws IOException {
        ByteArrayOutputStream byt=new ByteArrayOutputStream();
        String path="http://192.168.1.118:8080/mobileDemo/versionUpdateServlet";
        URL url=new URL(path);
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        byte[] b=new byte[1024];
        InputStream in=null;
        if (connection.getResponseCode()==200){
            in=connection.getInputStream();
            //in.read();
            int len=0;
            while ((len=in.read(b))!=1){
                byt.write(b,0,len);
            }
        }
        in.close();
        String s=byt.toString();
        return s;
    }
}
